package binaryTreeNode;

public class IsValidBST {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(1);
        root.right = new TreeNode(3);
        System.out.println(isValidBST(root));
    }

    public static boolean isValidBST(TreeNode root) {
        //使用中序遍历，判断是否为升序
        return inorder(root, Long.MIN_VALUE);
    }

    private static boolean inorder(TreeNode root, long minValue) {//long minValue:中序遍历遍历过程中的上一个节点的值
        if (root == null)
            return true;
        minValue = root.val;
        if (!inorder(root.left, minValue))
            return false;
        if (root.val < minValue)
            return false;
        if (!inorder(root.right, root.val))
            return false;
        return true;
    }


}
